package cn.tedu.egnotice1.controller;

import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeListQuery;
import cn.tedu.egnotice1.pojo.dto.NoticeUpdateParam;
import cn.tedu.egnotice1.pojo.entity.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String addNotice(NoticeAddParam noticeAddParam) {
        System.out.println("添加资讯：" + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdatedTime(new Date());
        noticeMapper.insertNotice( notice);
        return "新增资讯成功！";
    }

    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> selectNotice(NoticeListQuery noticeListQuery){
        System.out.println("查询资讯：" + noticeListQuery);
        List<NoticeListVO> noticeListVos = noticeMapper.selectNotice(noticeListQuery);
        return noticeListVos;
    }

    @GetMapping("/v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfoVO selectNoticeById(Long id){
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectNoticeById(id);
        return noticeDetailInfoVO;
    }

    @GetMapping("/v1/notice/del/{id}")
    @ResponseBody
    public String deleteNotice(@PathVariable("id") Long id){
        int rows = noticeMapper.deleteNotice(id);
        return "删除咨询成功";
    }

    @PostMapping("/v1/notice/update")
    @ResponseBody
    public String updateNotice(NoticeUpdateParam noticeUpdateParam){
        System.out.println("更新的资讯：" + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdatedTime(new Date());
        int i = noticeMapper.updateNoticeById(notice);
        return "更新资讯成功";
    }
}
